package com.huika.dealontop.support.http.LinePayHttp;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.http.FormBaseRequest;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.support.http.ServerJsonUnParseError;
import com.huika.dealontop.utils.DebugTool;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;

public class LineResultRequest<T> extends FormBaseRequest<LineRequestResult<T>> {
    private static final Type DEFAULT_TYPE = new TypeToken<RequestResult<String>>() {
    }.getType();

    private Gson mGson = new GsonBuilder().serializeNulls().create();
    ;
    private final Type typeOfT;

    public LineResultRequest(String url, Response.Listener<LineRequestResult<T>> listener, Response.ErrorListener errorListener, Type typeOfT) {
        this(Method.POST, url, listener, errorListener, typeOfT);
    }

    public LineResultRequest(int method, String url, Response.Listener<LineRequestResult<T>> listener, Response.ErrorListener errorListener, Type typeOfT) {
        super(method, url, listener, errorListener);
        addHeader("X-LINE-ChannelId", "1443610447");
        addHeader("X-LINE-ChannelSecret","23009dae120944136d23c5cd93426139");
        this.typeOfT = typeOfT;
    }




    /**
     * 复杂数据解析可外部传递gson
     *
     * @param mGson
     */
    public void setGson(Gson mGson) {
        this.mGson = mGson;
    }

    @Override
    protected Response<LineRequestResult<T>> parseNetworkResponse(NetworkResponse response) {
        try {
            String date = null;
            if (response.headers != null) date = response.headers.get("Date");

            String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            DebugTool.v("http", "ok_json:" + json);

            JSONObject jsonObject = new JSONObject(json);
            // 拦截服务器处理异常
            if (jsonObject.getInt("flag") == 0) {
                LineRequestResult<String> res = mGson.fromJson(json, DEFAULT_TYPE);
                return Response.error(new LineServerError(res));
            }
            LineRequestResult<T> res = mGson.fromJson(json, typeOfT);
            return Response.success(res, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (Exception e) {// 拦截服务器处理异常，判断flag
            try {
                String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                DebugTool.v("http", "error_json:" + json);
                return Response.error(new ServerJsonUnParseError(json));
            } catch (Exception er) {
            }
            return Response.error(new ParseError(e));
        }
    }

    /**
     * 追加请求地址
     */
    @Override
    protected void deliverResponse(LineRequestResult<T> response) {
//        response.url = getUrl();
        super.deliverResponse(response);
    }

    @Override
    public void deliverError(VolleyError error) {
//        error.url = getUrl();
        super.deliverError(error);
    }

}

